/*
 *	Perlin Production Co. Ltd. September 2012 (c).
 *			idc.c
 *		Image Data Converter
 */

#include "stdio.h"
#include "fdlib.h"
#include "paramlib.h"
#include "WinGDI.h"

#define	NUM_IP	2

#define	IP_FLIP		1	/* rotate video planes */
#define	FLIPDEF		0
#define	FLIPMIN		0
#define	FLIPMAX		1


INTPARM	params[NUM_IP];
BF	bf;
BC	bc;
BI	bi;
char	inname	[0x80];
char	outname	[0x80];


InitOpt(_params)
INTPARM	*_params;
{
	INTPARM	*ip;
	ip = _params;
	InitParm(ip++, 'v', VERBMIN, VERBDEF, VERBMAX, "verbosity");
	InitParm(ip++, 'f', FLIPMIN, FLIPDEF, FLIPMAX, "flip video planes");
}



int main(argc,argv)
int	argc;
char	**argv;
{
	FD		*fdIn;
	FD		*fdOut;
	int		verbose;
	int		isOS2;
	unsigned	size, numCol;
	unsigned	w,h,d;
	
	InitOpt(params);

	if (argc < 2) usage(0);
	
	if (*argv[1] == '-') trouble("Required parameter missing:","input[.BMP]");

	fdIn  = InitFD(ExtAdd(inname , argv[1], 0x7F, ".BMP"));
	fdOut = InitFD(ExtSub(outname, inname , 0x7F, ".BIN"));

	if (!strcmp(inname,outname))
		trouble("input and output names should differ",inname);
	
	if (argc > 2) OptParm(argv, 2, argc, params, NUM_IP);
	 
	verbose = params[IP_VERBOSE].ip_val; /* do not move this out */
	
	OpenFD(fdIn, FCB_READ);

	if ((read(fdIn->file,&bf,sizeof(_BF))) != sizeof(_BF))
			trouble("BITMAPFILEHEADER read error in",inname);
			
	Chk64K(&bf.bfSize   , "file size");
	Chk64K(&bf.bfOffBits, "offset to pixel data");
	
	if (verbose == VERBMAX)
	{
		printf("BITMAPFILEHEADER\n");
		printf("bfSize    =%u\n",bf.bfSize);
		printf("bfOffBits =%u\n",bf.bfOffBits);
	}
			
	
	if (bf.bfType != BF_TYPE) trouble("Not a bitmap file", inname);
	
	if ((read(fdIn->file,&bc,sizeof(_BC))) != sizeof(_BC))
			trouble("BITMAPCOREHEADER read error in",inname);

	isOS2 = (bc.bcSize) == sizeof(_BC);
	
	if (verbose) printf("%s bitmap\n",(isOS2 ? "OS/2" : "Windows"));
	
	
	if (isOS2)
	{
		PutBC(&bc, verbose);
		numCol	= 1 << bc.bcBitCount;

		w = bc.bcWidth;
		h = bc.bcHeight;
		d = bc.bcBitCount;
	}
	else
	{
		movmem(&bc, &bi, sizeof(_BC));
		
		Chk64K(&bi.biSize, "header size");
		
		size = bc.bcSize - sizeof(_BC);
		
		if ((read(fdIn->file, &bi.biPlanes,size)) != size)
				trouble("BITMAPINFOHEADER read error in",inname);
				
		PutBI(&bi, verbose);
		numCol	= 1 << bi.biBitCount;
		
		w = bi.biWidth;
		h = bi.biHeight;
		d = bi.biBitCount;
	}	
	

	CreatFD	(fdOut, FCB_WRITE);
	if (verbose) printf("Generating output file %s\n",outname);
	/*if (d == 4)*/
	BPP4Cvt	(fdOut,fdIn,bf.bfOffBits,w,h,0,verbose); 
	CloseFD(fdOut);
	FreeFD(fdIn);
	
	if (params[IP_FLIP].ip_val) RotPge(fdOut,verbose);

	return 	0;
}




usage(_text)
char* _text;
{
 printf("Perlin Production Co. Ltd. April 2014\n");
 printf("*** Image Data Converter ***, ver. 1.4\n");
 printf("Generates Vector-06c VRAM .BINary from BitMaP image\n");
 printf("\nTo display BMP image on Vector-06c, do the following:\n");
 rev_vid();
 printf("1. Use IDC (this) to convert .BMP image into Vector-06c .BIN format.\n");
 nrm_vid();
 printf("2. Use CLR utility to generate colorspace component levels .CLR file.\n");
 printf("3. Use APG utility to get image palette from .BMP and .CLR files.\n");
 printf("4. Use SHIM image viewer to display .BIN image in .PAL palette.\n");
 printf("Usage:\n");
 rev_vid();
 printf("IDC input[.BMP] [options]\n");
 nrm_vid();
 printf("-- Optional Parameters:\n");
 printf("[-v[%d]] - verbosity level   %d-%d\n", VERBDEF, VERBMIN, VERBMAX);
 printf("[-f[%d]] - flip video planes %d-%d\n", FLIPDEF,FLIPMIN,FLIPMAX);
 
 if (_text) trouble(_text, 0);
 else exit(0);
}
